package com.bwt.top.bwt_ad_flutter.ad;

import android.app.Activity;

import android.util.Log;

import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;

import com.bwt.top.ad.bean.AdInfo;
import com.bwt.top.ad.impl.ServiceAd;
import com.bwt.top.ad.listener.BannerAdListener;
import com.bwt.top.bwt_ad_flutter.AdErrorUtil;
import com.bwt.top.bwt_ad_flutter.config.BwtAdEventConfig;
import com.bwt.top.bwt_ad_flutter.manager.BwtAdManager;
import com.bwt.top.bwt_ad_flutter.model.AdParams;
import com.bwt.top.bwt_ad_flutter.util.LogUtil;
import com.bwt.top.exception.AdError;

public class BwtServiceAd extends BannerAdListener {

    @NonNull
    private final AdParams adParams;

    private ServiceAd serviceAd;

    private FrameLayout adContainer;

    public BwtServiceAd(Activity activity, @NonNull AdParams adParams, boolean loadThenShow){
        this.adParams = adParams;
        this.adContainer = new FrameLayout(activity);
//        adContainer.setBackgroundColor(0xFFFF0000);
        adContainer.setX(0);
        adContainer.setY(0);
        float density = activity.getResources().getDisplayMetrics().density;
        Double adWidth = adParams.getAdWidth();
        Double adHeight = adParams.getAdHeight();
        int width = adWidth != null && adWidth > 0 ? (int) (density * adWidth) : ViewGroup.LayoutParams.MATCH_PARENT;
        int height = adHeight != null && adHeight > 0 ? (int) (density * adHeight) : ViewGroup.LayoutParams.WRAP_CONTENT;
        adContainer.setLayoutParams(new ViewGroup.LayoutParams(width, height));
        this.serviceAd = new ServiceAd(activity,adContainer);
    }

    public void loadAd(){
        this.serviceAd.setAdId(adParams.getPosId());
        serviceAd.setAutoRefreshInterval(30);
        serviceAd.showCloseView();
        serviceAd.setAdListener(this);
        serviceAd.loadAd();
    }

    @Override
    public void onAdReceive(AdInfo adInfo) {
        Log.d("serviceAd","开始加载广告--"+adInfo);
        BwtAdManager.getInstance().postAdEvent(BwtAdEventConfig.KEY_AD_EVENT_ON_AD_LOAD, adParams);
    }

    @Override
    public void onAdExpose(AdInfo adInfo) {
        LogUtil.log("广告被展示，adId: " + adParams.getAdId());
        BwtAdManager.getInstance().postAdEvent(BwtAdEventConfig.KEY_AD_EVENT_ON_AD_EXPOSE, adParams);
    }

    @Override
    public void onAdClick(AdInfo adInfo) {
        LogUtil.log("广告被点击，adId: " + adParams.getAdId());
        BwtAdManager.getInstance().postAdEvent(BwtAdEventConfig.KEY_AD_EVENT_ON_AD_CLICK, adParams);
    }

    @Override
    public void onAdClose(AdInfo adInfo) {
        LogUtil.log("广告被关闭，adId: " + adParams.getAdId());
        BwtAdManager.getInstance().postAdEvent(BwtAdEventConfig.KEY_AD_EVENT_ON_AD_CLOSE, adParams);
        release();
    }

    @Override
    public void onAdFailed(AdError adError) {
        LogUtil.log("广告获取失败，adId: " + adParams.getAdId() + ", error: " + AdErrorUtil.getError(adError));
        BwtAdManager.getInstance().postAdEvent(BwtAdEventConfig.KEY_AD_EVENT_ON_AD_FAILED, adParams, AdErrorUtil.getError(adError));
    }

    public FrameLayout getAdContainer() {
        return adContainer;
    }

    /**
     * 释放广告
     */
    public void release() {
        if (serviceAd != null) {
            serviceAd.release();
            serviceAd = null;
        }
    }
}
